package com.wt.struggle.goodbye.user.activity

import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.design.widget.TabLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.sq.activity.PickDetailsActivity
import com.wt.struggle.goodbye.sq.adapter.PickUserAdapter
import com.wt.struggle.goodbye.user.adapter.FansAdapter
import kotlinx.android.synthetic.main.focus_on_layout.*
import kotlinx.android.synthetic.main.head.*
import org.json.JSONObject


/**
 * 关注
 */
class FocusOnActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {

            Send.GET_COLLECT_USER_CODE -> {
                removeLoadDialog()
                refreshView.isRefreshing = false
                if (netInfo.code == Config.SUCCESS) {
                    val data = netInfo.dataResult
                    val arr = gson!!.fromJson<ArrayList<ProUserInfo>>(
                        data,
                        object : TypeToken<ArrayList<ProUserInfo>>() {}.type
                    )
                    if (arr.size != 0) {
                        if (isLoad) {
                            adapter.updateData(arr)
                        } else {
                            adapter.updateDataClear(arr)
                        }

                    }

                    page++

                }
            }

            Send.ATTENTION_CODE -> {

                if (netInfo.code == Config.SUCCESS) {
                    list.removeAt(indexPosition)
                    adapter.notifyItemRemoved(indexPosition)
                    showToastShort("取消成功")
                }
            }
        }

    }

    var code: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.focus_on_layout)

        tvTitle.text = "我的关注"

        code = intent.getIntExtra("code", 0)

        imageBack.setOnClickListener {
            finish()
        }

        initAdapter()

        userTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                tabCode = tab!!.position
                get(tabCode)
            }
        })

        refreshView.setOnRefreshListener {
            get(tabCode)
        }

        refreshView.onLoadMore(focusRecyclerView) {
            get(tabCode)
        }

        get(0)

    }

    var page = 1
    var tabCode = 0
    var isLoad = false

    fun get(code: Int) {
        send!!.getCollectUser(page, code, handler)
        showLoadDialog("获取中")

    }

    var indexPosition: Int = 0

    fun collect(position: Int) {
        this.indexPosition = position
        val info = list[position]
        send!!.attentionUser(info, handler)

    }

    val list = arrayListOf<ProUserInfo>()
    lateinit var adapter: FansAdapter

    private fun initAdapter() {

        focusRecyclerView.layoutManager = LinearLayoutManager(this)
        focusRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        adapter = FansAdapter(this, list)
        adapter.joupCode = code
        focusRecyclerView.adapter = adapter

        adapter.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {
                val info = list[position]
                val intent = Intent(this@FocusOnActivity, UserInfoActivity::class.java)
                intent.putExtra("user", info)
                startActivity(intent)

            }

            override fun onLongClick(position: Int) {

            }

        }
    }
}